package mx.com.initzero.ecommerce.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import mx.com.initzero.ecommerce.dao.SearchDao;
import mx.com.initzero.ecommerce.dto.Producto;
import mx.com.initzero.ecommerce.vo.ProductoVo;

@Repository
public class SearchDaoImpl extends JdbcDaoSupport implements SearchDao{

	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Autowired
	public SearchDaoImpl(DataSource dataSource){
		setDataSource(dataSource);
		this.namedParameterJdbcTemplate= new NamedParameterJdbcTemplate(dataSource);
	}
	@Override
	public List<ProductoVo> selectBySearch(String busqueda) {
		// TODO Auto-generated method stub
		busqueda = "'%" + busqueda + "%'";
		String sql = "select * from producto where descripcion Like "
				+ busqueda;
		
		return getJdbcTemplate().query(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(ProductoVo.class));
	}

	@Override
	public List<String> selectByAutoComplete(String busqueda) {
		// arreglar esto ya que no se hace por parametro
		busqueda = "'%" + busqueda + "%'";
		String sql = "select descripcion from producto where descripcion Like "
				+ busqueda;

		List<String> productos = new ArrayList<String>();

		List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);

		for (Map row : rows) {
			productos.add(row.get("descripcion") + "");
		}

		return productos;
		
	}

}
